<template>
  <div class="x-profile-container">
    <el-row class="x-aside hidden-sm-and-down">
      <div class="profile-container">
        <div class="op">
          <!-- <i :class="collapsed ? 'el-icon-s-unfold': 'el-icon-s-fold'" @click="changeCollapse"></i> -->
        </div>
        <div class="profile">
          <!-- 头像、座右铭 -->
          <div class="avatar">
            <el-upload
              class="upload_avatar"
              :action="upload_qny_url"
              :data="qny"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img :src="user.avatar" alt="avatar" />
              <div class="mask"><i class="el-icon-picture-outline"></i></div>
            </el-upload>
            <p class="username">{{ user.name }}</p>
          </div>
          <!-- <div class="motto">
              <p>Talk is cheap, show me the code!</p>
            </div>-->
        </div>
        <div>
          <!--  -->
        </div>
      </div>
      <nav class="nav-box">
        <el-menu
          :default-active="activedView"
          active-text-color="#5fb878"
        >
          <el-menu-item
            :index="m.id"
            v-for="(m, index) in menu"
            :key="index"
          >
            <template slot="title">
              <router-link :to="m.id">
                <i :class="m.icon"></i>
                <span>{{ m.title }}</span>
              </router-link>
            </template>
          </el-menu-item>
          <el-menu-item @click="exit">
            <template>
              <i class="el-icon-s-promotion"></i>
              <span>退 出</span>
            </template>
          </el-menu-item>
        </el-menu>
      </nav>
    </el-row>
    <div class="x-main">
      <div class="x-body">
        <transition mode="out-in" name="show">
          <router-view></router-view>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
import Editor from "@/views/profile/editor";
import UserInfo from "@/views/profile/userInfo";
import ArticleList from "@/views/profile/articleList";
import BlogSetting from "@/views/profile/blogSetting";
import request from "@/utils/request";
import validator from "@/utils/validator";
import jsCookie from 'js-cookie';

export default {
  name: "",
  components: {},
  data() {
    return {
      // 导航菜单
      menu: [
        {
          id: "userInfo",
          submenu: null,
          icon: "el-icon-user-solid",
          title: "个人资料",
        },
        {
          id: "editor",
          submenu: null,
          icon: "el-icon-edit-outline",
          title: "撰写文章",
        },
        {
          id: "articleList",
          submenu: null,
          icon: "el-icon-document",
          title: "文章列表",
        },
        {
          id: "blogSetting",
          submenu: null,
          icon: "el-icon-s-tools",
          title: "博客管理",
        },
      ],
      // 当前显示的页面
      activedView: "userInfo",
      // 上传凭证
      qny: {
        key: "",
        token: "",
      },
      // 上传域名
      upload_qny_url: "http://upload-z2.qiniup.com",
      base_url: "http://img.xsirl.cn/",
    };
  },
  computed: {
    user() {
      return this.$store.getters.user;
    },
  },
  watch: {
    $route: {
      handler: function (to) {
        this.activedView = to.name;
      },
      immediate: true,
    },
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    /**
     * 上传成功后更换头像
     */
    handleAvatarSuccess(res, file) {
      // console.log(file);
      const avatar = this.base_url + res.key;
      this.$store.commit("user/SET_AVATAR", avatar);
      request.post("/user/update", { avatar: avatar }).then((res) => {});
    },
    /**
     * 上传前：验证文件格式、大小（格式：jpeg、png；大小：2M）
     */
    beforeAvatarUpload(file) {
      // console.log(file);
      this.qny.key = this.user.name + "_avatar";
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size < 2 * 1024 * 1024;

      // 文件格式校验
      if (!isJPG) {
        this.$message({
          type: "error",
          message: "上传头像图片只能是 JPG、PNG 格式!",
          duration: 1500,
        });
        return false;
      }
      // 文件大小
      if (!isLt2M) {
        this.$message({
          type: "error",
          message: "上传头像图片大小不能超过 2MB!",
          duration: 1500,
        });
        return false;
      }

      return true;
    },
    /**
     * 获取上传凭证，覆盖上传
     */
    getQnyToken() {
      request.get(`/qny/token`).then((res) => {
        this.qny.token = res.token;
        jsCookie.set("qnyToken", this.qny.token, { path: "/profile" });
      });
    },
    exit() {
      this.$store.dispatch("user/logout").then(() => {
        this.$router.push("/home");
      });
    },
  },
  created() {
    if(!jsCookie.get("qnyToken")) {
      this.getQnyToken();
    }
  },
};
</script>
  
<style scoped>
  .el-menu-item.is-active > a {
    color: inherit;
  }
  .el-menu-item > a {
    display: block;
    transition: color .3s
  }
  /* 动画效果 */
  .show-enter,
  .show-leave-to {
    transition: all 0.3s;
    opacity: 0;
  }
  .show-enter-active,
  .show-leave {
    transition: all 0.3s;
  }

  .x-profile-container {
    display: flex;
    min-height: calc(100vh - 50px);
    max-height: calc(100vh - 50px);
    overflow: hidden;
  }

  .x-aside {
    width: 200px;
    box-shadow: 1px 0 10px -9px #333;
    transition: all 0.3s;
    background-color: #fff;
    flex-shrink: 0;
  }
  .x-aside .el-menu {
    border-right: none;
  }
  .x-aside .nav-box {
    max-height: 480px;
    overflow-y: scroll;
    overflow-x: hidden;
  }
  .x-aside .nav-box::-webkit-scrollbar {
    width: 0;
    background-color: transparent;
  }
  .x-aside .nav-box::-webkit-scrollbar-track {
    background-color: transparent;
  }
  .x-aside .nav-box::-webkit-slider-thumb {
    background-color: transparent;
  }

  .profile-container {
    background-color: #074b60;
    position: relative;
    z-index: 1;
    box-shadow: 0 5px 10px -9px #333;
  }
  .profile-container .op {
    position: absolute;
    top: 0px;
    right: 0px;
    display: none;
    padding: 5px;
    color: #ccc;
    transition: all 0.3s;
    font-size: 1.5rem;
  }
  .profile-container .op:hover {
    color: #eee;
  }
  .profile-container:hover .op {
    display: block;
  }
  .profile {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    height: 150px;
  }
  .profile .avatar {
    text-align: center;
  }
  .profile .avatar .upload_avatar {
    position: relative;
    overflow: hidden;
    border-radius: 50%;
  }
  .profile .avatar .upload_avatar .mask {
    position: absolute;
    left: 0;
    right: 0;
    font-size: 50px;
    border-radius: 50%;
    transform: translateY(0%);
    /* transition: transform .1s ease-in-out; */
    color: rgba(255, 255, 255, 0.5);
    background-color: rgba(0, 0, 0, 0.5);
  }
  .profile .avatar .upload_avatar:hover .mask {
    transform: translateY(-100%);
  }
  .profile .avatar img {
    width: 4.57rem;
    height: 4.57rem;
    border-radius: 50%;
    -webkit-user-select: none;
  }

  .profile .motto {
    text-align: center;
    color: #5f785f;
    margin-top: 5px;
  }

  .username {
    position: relative;
    color: #fff;
    margin-top: 5px;
  }
  .username::after {
    content: "";
    width: 100%;
    height: 0.5px;
    background: linear-gradient(
      90deg,
      transparent,
      #5fb878 25%,
      #5fb878 75%,
      transparent
    );
    position: absolute;
    bottom: 0;
    left: 0;
  }

  .x-main {
    /* position: fixed; */
    /* margin-left: 14.29rem; */
    /* overflow: hidden; */
    /* min-height: 100%; */
    /* width: calc(100% - 14.29rem); */
    flex-grow: 1;
    z-index: 1;
    background-color: #eee;
  }
  .x-main .x-body {
    position: relative;
    padding: 15px;
    box-sizing: border-box;
    max-height: calc(100vh - 50px);
    overflow: scroll;
  }
  .x-main .x-body::-webkit-scrollbar {
    background-color: transparent;
    width: 6px;
    height: 0;
  }
  .x-main .x-body::-webkit-scrollbar-track {
    background-color: transparent;
  }
  .x-main .x-body::-webkit-scrollbar-thumb {
    background-color: #b7b7b7;
    border-radius: 3px;
    visibility: hidden;
  }
  .x-main .x-body:hover::-webkit-scrollbar-thumb {
    visibility: visible;
  }
</style>